package com.investmenttechnologyservices.services;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.investmenttechnologyservices.util.Util;

public class DbService {
	public void bulkInsert(String type) {
	}
	
	public void insertIssuers() {
		DatastoreService s = DatastoreServiceFactory.getDatastoreService();
		for (int i=1; i<=10; i++) {
			Entity ent = createIssuer("issr"+i,"Issuer Name"+i);
			s.put(ent);
		}
	}
	
	public void deleteIssuers() {
		deleteAll("issuer");
	}
	
	public void deleteAll(String kind) {
		System.err.println("delete all of kind = " + kind);
		DatastoreService s = DatastoreServiceFactory.getDatastoreService();
		PreparedQuery pq = s.prepare(new Query(kind));
		for (Entity ent : pq.asIterable()) {
			s.delete(ent.getKey());
		}
	}
	
	public void queryAll(String kind) {
		DatastoreService s = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query(kind);
		printAll("query all of kind = " + kind, s, q);
	}

	private void printAll(String msg, DatastoreService s, Query q) {
		System.err.println("> ------ " + msg + "--------");
		PreparedQuery pq = s.prepare(q);
		int count = 0;
		for (Entity ent : pq.asIterable()) {
			Util.printEntity(ent);
			count ++;
		}
		System.err.println("> ------ total = " + count + "--------");
	}
	
	public void insertInst() {
		DatastoreService s = DatastoreServiceFactory.getDatastoreService();
		for (int i=1; i<=100; i++) {
			s.beginTransaction();
			int issrNum = (i % 10) + 1;
			Entity ent = createInstrument("inst"+i, "Inst Name"+i, null, "issr"+issrNum, null);
			s.put(ent);
		}
	} 	
	
	public void deleteInst() {
		deleteAll("instrument");
	} 	
	
	public void insertSymbols() {
		
	} 	
	
	public void deleteSymbols() {
		
	} 	
	
	public void insertExch() {
		
	} 	
	
	public void deleteExch() {
		
	}
	
	public void q() {
		deleteAll("instrument");
		deleteAll("issuer");
		
		insertIssuers();
		insertInst();
		
		queryAll("instrument");
		queryAll("issuer");

		DatastoreService s = DatastoreServiceFactory.getDatastoreService();

		Query q = new Query("instrument");
		q.setAncestor(KeyFactory.createKey("issuer", "issr4"));
		printAll("all instrument for issuer=issr4", s, q);
	}

	private Entity createIssuer(String id, String name) {
		Key key = KeyFactory.createKey("issuer", id);
		Entity ent = new Entity(key);
		ent.setProperty("name", name);
		return ent;
	}
	
	private Entity createInstrument(String id, String name, Long firmId, String issuerId, Boolean cashTraded) {
		Key key;
		if (issuerId != null) {
			key = KeyFactory.createKey(KeyFactory.createKey("issuer", issuerId), "instrument", id);
		} else {
			key = KeyFactory.createKey("instrument", id);
		}
		Entity ent = new Entity(key);
		ent.setProperty("name", name);
		if (firmId != null) {
			ent.setProperty("firmId", firmId);
		}
		if (cashTraded != null) {
			ent.setProperty("cashTraded", cashTraded);
		}
		return ent;
	}
	
}
